package com.trimps.wifi.service.impl;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.trimps.wifi.mapper.DataMapper;
import com.trimps.wifi.pojo.Account;
import com.trimps.wifi.service.IDataService;

@Service("dataService")
public class DataServiceImpl implements IDataService {
	@Autowired
	private DataMapper dataMapper;	

	@Override
	public int getAccountCount(String keyword) {
		return dataMapper.getAccountCount(keyword);
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public Map<String, Object> getAllAccount(String keyword, int currentPage, int rowPerPage) {
		List<Account> accounts = dataMapper.getAllAccount(keyword, currentPage, rowPerPage);
		Map<String, Object> rtn = new LinkedHashMap<String, Object>();
		for(Account account : accounts) {
			if(rtn.get(account.getAccountMac()) == null) {
				rtn.put(account.getAccountMac(), new LinkedHashMap<String, Object>());
			}
			Map<String, Object> vals = (Map)rtn.get(account.getAccountMac());
			String codeName = String.valueOf(account.getAccountType());
			if(account.getAccountCode() != null && account.getAccountCode().length() > 0) {
				if(vals.get(codeName) == null) {
					vals.put(codeName, account.getAccountCode());
				}else if(vals.get(codeName) != account.getAccountCode()){
					vals.put(codeName, vals.get(codeName) + "," + account.getAccountCode());
				}
			}
			if(vals.get("captureTime") == null) {
				vals.put("captureTime", account.getCaptureTime());
			}else if(account.getCaptureTime().getTime() > ((Date)vals.get("captureTime")).getTime()){
				vals.put("captureTime", account.getCaptureTime());
			}
			vals.put("brand", account.getBrand());
		}
		
		return rtn;
	}

}
